package com.uenpay.im.panel.gif;

import android.text.TextUtils;

import com.uenpay.im.IMApplication;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Vector;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 *
 */
public class ViewGif {

	public String file_type_name;
	private Vector<ViewGifEntity> fileNames = null;

	public ViewGif(String _file_type_name) {
		file_type_name = _file_type_name;
		// initResourceId(_file_type_name);
		initResourceIdByAssets(_file_type_name);
	}

	public String getFileTypeName() {
		return file_type_name;
	}

	public ViewGif getEmoji(int index) {
		if (fileNames != null && fileNames.size() > index) {
			return new ViewGif(fileNames.get(index).fileNames);
		} else {
			return null;
		}
	}

	/**
	 * 获取gif对应名称
	 *
	 * @param index
	 * @return
	 */
	public String getGifName(int index) {
		if (fileNames != null && fileNames.size() > index) {
			return fileNames.get(index).fileNames;
		} else {
			return "";
		}
	}

	/**
	 * 获取表情的hash默认表情：""
	 * @param index
	 * @return
	 */
	public String getGifFileHash(int index){
		if (fileNames != null && fileNames.size() > index) {
			return fileNames.get(index).fileHash;
		}else {
			return "";
		}
	}
	/**
	 * 获取表情的hash默认表情：""
	 * @param index
	 * @return
	 */
	public String getGifFileId(int index){
		if (fileNames != null && fileNames.size() > index) {
			return fileNames.get(index).fileId;
		}else {
			return "";
		}
	}

	public int size() {
		return fileNames == null ? 0 : fileNames.size();
	}

	// private void initResourceId(String file_type_name) {
	// if (TextUtils.isEmpty(file_type_name)) {
	// return;
	// }
	// // 该表情的目录
	// String emoji_path = Environment.getExternalStorageDirectory()
	// .getAbsolutePath() + "/downloadEmoji/" + file_type_name + "/";
	// File file = new File(emoji_path);
	// File[] files = file.listFiles();
	// if (files != null && files.length > 0) {
	// fileNames = new Vector<String>();
	// int index = 0;
	// for (int i = 0, size = files.length; i < size; i++) {
	// index++;
	// String count_index = "00";
	// if (index >= 10) {
	// count_index = "0";
	// }
	//
	// fileNames.add(emoji_path + "/" + file_type_name + count_index
	// + index + ".png");
	// }
	// }
	// if (files != null) {
	// files.clone();
	// }
	// }

	/**
	 * 或资源文件表情
	 *
	 * @param file_type_name
	 *            表情类型名称
	 */
	private void initResourceIdByAssets(String file_type_name) {
		if (file_type_name.equals("yutumei")) {
			String str = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24";
			String[] strs = str.split(",");
			fileNames = new Vector<ViewGifEntity>();
			for (int i = 0, size = strs.length; i < size; i++) {
				String resourceName = "yutumei_" + strs[i] + "_cover";
				ViewGifEntity mViewGifEntity = new ViewGifEntity();
				mViewGifEntity.fileNames = resourceName;
				fileNames.add(mViewGifEntity);
			}

		} else if (file_type_name.equals("like")) {
			/** 获取本地保存的表情 **/
			fileNames = new Vector<ViewGifEntity>();
			String customJson = IMApplication.config
					.getCustomFaceJson(IMApplication.getCustId());
			if (!TextUtils.isEmpty(customJson)) {
				try {
					JSONArray jsonArray = new JSONArray(customJson);
					if (jsonArray != null && jsonArray.length() > 0) {
						for (int i = 0, size = jsonArray.length(); i < size; i++) {
							JSONObject jsonObject = jsonArray.optJSONObject(i);
							ViewGifEntity mViewGifEntity = new ViewGifEntity();
							mViewGifEntity.fileHash = jsonObject.optString("filehash");
							mViewGifEntity.fileId = jsonObject.optString("fileid");
							mViewGifEntity.fileNames = jsonObject.optString("customefaceurl");
							fileNames.add(mViewGifEntity);
						}

					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			ViewGifEntity  mViewGifEntity = new ViewGifEntity();
			mViewGifEntity.fileNames = "loac_add";
			fileNames.add(0,mViewGifEntity);
		}
	}

}
